// 初始化云开发环境
wx.cloud.init({
  env: 'your_cloud_env_id',
  traceUser: true
})
const App = getApp();
Page({



  onShareAppMessage() {
    return {
      title: '专业简历制作工具 - 海量模板任你选',
      path: '/pages/index/index',
      imageUrl: 'https://qzruocheng.oss-cn-beijing.aliyuncs.com/resume_assets/assets/icons/logo.png'
    }
  },

  data: {
    touchedBubbleIndex: -1, // 记录当前点击的气泡索引
    searchText: '', // 搜索内容
    // 原有数据
    bannerList: [],
    bubbleTags: [], // 新增标签数据
    featureList: [
      {
        icon: 'star',
        title: '智能简历生成',
        desc: 'AI助你打造专业简历'
      },
      {
        icon: 'light',
        title: '海量案例参考',
        desc: '精选优质简历模板'
      },
      {
        icon: 'edit',
        title: '一键优化建议',
        desc: '专业HR指导优化'
      }
    ],

    // 新UI数据-推荐模板
    templates: [
      // {
      //   id: 1,
      //   name: "简约商务风简历",
      //   tag: "金融/咨询/法律",
      //   image: "/assets/images/templates/template1.png"
      // },

    ],
    industries: [
      {
        id: 1,
        name: "IT/互联网",
        icon: "https://qzruocheng.oss-cn-beijing.aliyuncs.com/resume_assets/assets/icons/industry/it.png",
        bgColor: "#E8F3FF"
      },
      {
        id: 2,
        name: "金融",
        icon: "https://qzruocheng.oss-cn-beijing.aliyuncs.com/resume_assets/assets/icons/industry/finance.png",
        bgColor: "#FFF3E8"
      },
      {
        id: 3,
        name: "教育",
        icon: "https://qzruocheng.oss-cn-beijing.aliyuncs.com/resume_assets/assets/icons/industry/edu.png",
        bgColor: "#E8FFF3"
      },
      {
        id: 4,
        name: "医疗",
        icon: "https://qzruocheng.oss-cn-beijing.aliyuncs.com/resume_assets/assets/icons/industry/medical.png",
        bgColor: "#F3E8FF"
      },
      {
        id: 5,
        name: "销售",
        icon: "https://qzruocheng.oss-cn-beijing.aliyuncs.com/resume_assets/assets/icons/industry/sales.png",
        bgColor: "#FFE8E8"
      },
      {
        id: 6,
        name: "市场",
        icon: "https://qzruocheng.oss-cn-beijing.aliyuncs.com/resume_assets/assets/icons/industry/marketing.png",
        bgColor: "#E8FFFF"
      },
      {
        id: 7,
        name: "设计",
        icon: "https://qzruocheng.oss-cn-beijing.aliyuncs.com/resume_assets/assets/icons/industry/design.png",
        bgColor: "#FFF8E8"
      },
      {
        id: 8,
        name: "人力资源",
        icon: "https://qzruocheng.oss-cn-beijing.aliyuncs.com/resume_assets/assets/icons/industry/hr.png",
        bgColor: "#F3FFE8"
      },
      {
        id: 9,
        name: "法律",
        icon: "https://qzruocheng.oss-cn-beijing.aliyuncs.com/resume_assets/assets/icons/industry/more.png",
        bgColor: "#F5F5F5"
      }
    ],
    capsuleTop: "", //胶囊距离屏幕顶部的距离
    capsuleHeight: "", //胶囊高度
    navbarHeight: ""//导航栏高度
  },

  onLoad() {
    this.getBannerData()
    this.getRecommendTemplates()
    this.loadBubbleTags()
    this.setData({
      capsuleTop: App.globalData.capsule.top,
      capsuleHeight: App.globalData.capsule.height,
      navbarHeight: (App.globalData.capsule.top - App.globalData.system.statusBarHeight) * 2 + App.globalData.capsule.height + App.globalData.system.statusBarHeight,
    })

  },

  // 加载标签数据
  loadBubbleTags() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })

    wx.cloud.callFunction({
      name: 'getBubbleTags',
      config: {
        env: 'your_cloud_env_id'
      },
      success: res => {
        if (res.result && res.result.code === 0) {
          this.setData({
            bubbleTags: (res.result.data || []).filter(item => item.enable !== false)
          })
        }
      },
      fail: err => {
        console.error('获取标签数据失败:', err)
        wx.showToast({
          title: '获取标签数据失败',
          icon: 'none'
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  // 获取推荐模板数据
  async getRecommendTemplates() {
    try {
      wx.showLoading({
        title: '加载中...',
        mask: true
      })

      const res = await wx.cloud.callFunction({
        name: 'getRecommendTemplates',
        config: {
          env: 'your_cloud_env_id'
        }
      })

      if (res.result && res.result.code === 0 && res.result.data.length) {
        // 合并推荐模板和本地模板数据
        const templates = [...res.result.data, ...this.data.templates].filter(item => item.enable !== false)
        this.setData({ templates })
      }
    } catch (err) {
      console.error('获取推荐模板失败:', err)
      wx.showToast({
        title: '获取模板数据失败',
        icon: 'none'
      })
    } finally {
      wx.hideLoading()
    }
  },
  navigateToCodeBuddy: function () {
    wx.setClipboardData({
      data: 'https://mc.tencent.com/HRVjVcS5',
      success: () => {
        wx.showToast({
          title: '链接已复制，请在浏览器中打开',
          icon: 'none'
        });
      }
    });
  },
  navigateToCloudBase: function () {
    wx.setClipboardData({
      data: 'https://docs.cloudbase.net/ai/cloudbase-ai-toolkit/?from=csdn-hackathon-2025',
      success: () => {
        wx.showToast({
          title: '链接已复制，请在浏览器中打开',
          icon: 'none'
        });
      }
    });
  },
  // 获取轮播图数据
  async getBannerData() {
    try {
      const res = await wx.cloud.callFunction({
        name: 'getOpenId'
      })
      console.log('云函数调用成功', res)
    } catch (err) {
      console.error('云函数调用失败', err)
    }
  },

  // 跳转到简历生成页
  navigateToResumeAI() {
    wx.navigateTo({
      url: '/pages/resume-ai/resume-ai'
    })
  },


  // 跳转到简历生成页
  navigateToResumeAI() {
    wx.navigateTo({
      url: '/pages/resume-ai/resume-ai'
    })
  },

  // 新建简历
  handleCreateResume() {
    const now = new Date();
    const deadline = new Date('2025-08-05');
    if (now > deadline) {
      wx.navigateTo({
        url: '/pages/resume-edit/resume-edit'
      });
    } else {
      wx.showToast({
        title: '该功能暂未开放',
        icon: 'error',
        duration: 1000
      })
    }


  },

  // 我的简历
  handleMyResume() {
    wx.showToast({
      title: '该功能暂未开发',
      icon: 'error',
      duration: 1000
    })

  },

  // 模板中心
  handleTemplateCenter() {
    wx.navigateTo({
      url: '/pages/indexTemplateDetail/indexTemplateDetail'
    });
  },

  // 行业分类点击
  handleIndustryTap(e) {
    const { id, name } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/sectionList/sectionList?name=${name}`
    });
  },

  // 气泡点击事件处理
  handleBubbleTap: function (e) {
    const id = e.currentTarget.dataset.id;
    const index = e.currentTarget.dataset.index;
    const tag = this.data.bubbleTags.find(item => item.id === id);
    this.setData({
      touchedBubbleIndex: index
    });
    // 1秒后取消激活状态
    setTimeout(() => {
      this.setData({ touchedBubbleIndex: -1 });
    }, 1000);

    // 跳转到模板列表页
    wx.navigateTo({
      url: `/pages/bubbleTemplateList/bubbleTemplateList?keyword=${encodeURIComponent(tag.mapNode)}`
    });
  },

  // 跳转到模板列表页
  navigateToTemplates() {
    wx.navigateTo({
      url: '/pages/indexTemplateDetail/indexTemplateDetail'
    });
  },

  // 处理搜索输入
  handleSearchInput(e) {
    this.setData({
      searchText: e.detail.value.trim()
    });
  },

  // 处理搜索框失焦
  handleSearchBlur() {
    if (this.data.searchText.trim()) {
      wx.navigateTo({
        url: `/pages/indexTemplateDetail/indexTemplateDetail?keyword=${encodeURIComponent(this.data.searchText.trim())}`
      });
      this.setData({
        searchText: ''
      });
    }
  }
})